package com.hs.jhzd.service.task;

import com.hs.jhzd.entity.Task;
import com.hs.jhzd.repository.TaskRepository;
import com.hs.jhzd.utils.ToolUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@PropertySource(value="classpath:application.properties",encoding = "UTF-8")
public class TaskService {

  @Autowired
  private TaskRepository taskRepository;

  @Autowired
  private ScheduleService scheduleService;
  @Value("${jsbh}")
  private String jsbh;
  @Value("${jsmc}")
  private String jsmc;

  /**
   *  * 初始化所有任务.
   */
  @PostConstruct
  public void init() {
    scheduleService.start();
    List<Task> tasks = findAll();
    for (Task task : tasks) {
      try {
        String taskId = scheduleService.addTask(task);
        log.debug("init task : taskId = {},task orderName = {}", taskId, task.getOrderName());
        task.setTaskId(taskId);
        taskRepository.save(task);
      } catch (Exception ex) {
        log.error("初始化定时任务异常", ex);
      }
    }
    startFingerprintTask();
  }

  private void startFingerprintTask() {
    Task task = new Task();
    task.setOrderName(Task.FINGERPRINT);
    String taskId = scheduleService.addTask(task);
    log.debug("初始化指纹定时任务 : init task : taskId = {},task orderName = {}", taskId, Task.FINGERPRINT);

  }

  /**
   * 根据监所编号查找.
   *
   * @param prisonId 监所编号
   * @return 任务列表
   */
  public List<Task> findAll() {
    List<Task> tasks = taskRepository.findAll();
    for (Task task : tasks) {
      try {
        if (StringUtils.isBlank(task.getTaskId())) {
          task.setActive(false);
        }
        task.setActive(scheduleService.getById(task.getTaskId()) != null ? true : false);
      } catch (Exception ex) {
        log.error("获取定时任务异常", ex);
      }
    }
    return tasks;
  }



  /**
   * 新增任务.
   *
   * @param task 任务 @ 启动任务异常
   */
  public void insert(Task task) {
    if (task != null) {
      task.setCreateTime(new Date());
      String taskId = scheduleService.addTask(task);
      task.setTaskId(taskId);
      String oldId = taskRepository.findZybh();
      task.setId(ToolUtils.CreatePrimaryKey(oldId));
      task.setJsbh(jsbh);
      task.setJsmc(jsmc);
      taskRepository.save(task);
    }
  }

  /**
   * 删除任务.
   *
   * @param id 任务id
   */
  public void delete(@NonNull String id) {
    Task task = findById(id);
    if (task == null) {
      return;
    }
    scheduleService.delTask(task.getTaskId());
    taskRepository.deleteById(id);
  }

  /**
   * 根据id查找任务.
   * 
   * @param id task id
   * @return task
   */
  public Task findById(@NonNull String id) {
    Optional<Task> task = taskRepository.findById(id);
    return task.isPresent() ? task.get() : null;
  }
}
